#!/usr/bin/env python3
"""
数据质量检查脚本
用于批量检查知识库数据质量，生成详细报告
"""

import os
import sys
import argparse
import json
from datetime import datetime
from pathlib import Path
from typing import List, Dict, Any
import pandas as pd

# 添加当前目录到Python路径
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from data_validation import DataValidator

class QualityChecker:
    """数据质量检查器"""
    
    def __init__(self, knowledge_base_path: str):
        self.knowledge_base_path = Path(knowledge_base_path)
        self.validator = DataValidator()
        self.check_results = []
        
        # 知识库文件映射
        self.knowledge_files = {
            'fault_cases': 'fault_cases_template.csv',
            'solutions': 'solutions_template.csv', 
            'architecture': 'architecture_template.csv',
            'experience': 'experience_template.csv'
        }
    
    def run_quality_check(self) -> Dict[str, Any]:
        """运行完整的数据质量检查"""
        print("🔍 开始数据质量检查...")
        print(f"📁 知识库路径: {self.knowledge_base_path}")
        print("-" * 60)
        
        # 1. 检查目录结构
        self._check_directory_structure()
        
        # 2. 检查文件存在性
        self._check_file_existence()
        
        # 3. 验证CSV文件
        self._validate_csv_files()
        
        # 4. 生成质量报告
        quality_report = self._generate_quality_report()
        
        return quality_report
    
    def _check_directory_structure(self):
        """检查目录结构"""
        print("📂 检查目录结构...")
        
        required_dirs = [
            'fault_cases',
            'solutions', 
            'architecture',
            'experience'
        ]
        
        for dir_name in required_dirs:
            dir_path = self.knowledge_base_path / dir_name
            if dir_path.exists():
                print(f"  ✅ {dir_name}/ 目录存在")
            else:
                print(f"  ❌ {dir_name}/ 目录缺失")
                os.makedirs(dir_path, exist_ok=True)
                print(f"  📁 已创建 {dir_name}/ 目录")
    
    def _check_file_existence(self):
        """检查文件存在性"""
        print("\n📄 检查文件存在性...")
        
        for knowledge_type, filename in self.knowledge_files.items():
            file_path = self.knowledge_base_path / filename
            if file_path.exists():
                print(f"  ✅ {filename} 存在")
            else:
                print(f"  ❌ {filename} 缺失")
    
    def _validate_csv_files(self):
        """验证CSV文件"""
        print("\n🔍 验证CSV文件...")
        
        for knowledge_type, filename in self.knowledge_files.items():
            file_path = self.knowledge_base_path / filename
            if file_path.exists():
                print(f"\n📋 验证 {filename}...")
                result = self.validator.validate_csv_file(str(file_path), knowledge_type)
                self.check_results.append(result)
                
                # 显示验证结果
                status = "✅ 通过" if result.get('valid', False) else "❌ 失败"
                print(f"  状态: {status}")
                print(f"  数据量: {result.get('total_rows', 0)}行 x {result.get('total_columns', 0)}列")
                
                if result.get('errors'):
                    print(f"  错误数: {len(result['errors'])}")
                    for error in result['errors'][:3]:  # 只显示前3个错误
                        print(f"    - {error}")
                    if len(result['errors']) > 3:
                        print(f"    ... 还有 {len(result['errors']) - 3} 个错误")
                
                if result.get('warnings'):
                    print(f"  警告数: {len(result['warnings'])}")
                    for warning in result['warnings'][:2]:  # 只显示前2个警告
                        print(f"    - {warning}")
                    if len(result['warnings']) > 2:
                        print(f"    ... 还有 {len(result['warnings']) - 2} 个警告")
            else:
                print(f"  ⚠️  {filename} 不存在，跳过验证")
    
    def _generate_quality_report(self) -> Dict[str, Any]:
        """生成质量报告"""
        print("\n📊 生成质量报告...")
        
        # 统计信息
        total_files = len(self.check_results)
        valid_files = sum(1 for r in self.check_results if r.get('valid', False))
        total_errors = sum(len(r.get('errors', [])) for r in self.check_results)
        total_warnings = sum(len(r.get('warnings', [])) for r in self.check_results)
        total_rows = sum(r.get('total_rows', 0) for r in self.check_results)
        
        quality_score = (valid_files / total_files * 100) if total_files > 0 else 0
        
        report = {
            'timestamp': datetime.now().isoformat(),
            'knowledge_base_path': str(self.knowledge_base_path),
            'summary': {
                'total_files': total_files,
                'valid_files': valid_files,
                'invalid_files': total_files - valid_files,
                'quality_score': round(quality_score, 2),
                'total_errors': total_errors,
                'total_warnings': total_warnings,
                'total_data_rows': total_rows
            },
            'details': self.check_results,
            'recommendations': self._generate_recommendations()
        }
        
        return report
    
    def _generate_recommendations(self) -> List[str]:
        """生成改进建议"""
        recommendations = []
        
        if not self.check_results:
            recommendations.append("📝 建议：添加知识库数据文件")
            return recommendations
        
        # 基于验证结果生成建议
        error_count = sum(len(r.get('errors', [])) for r in self.check_results)
        warning_count = sum(len(r.get('warnings', [])) for r in self.check_results)
        
        if error_count > 0:
            recommendations.append(f"🔧 优先修复 {error_count} 个数据错误")
        
        if warning_count > 0:
            recommendations.append(f"⚠️  处理 {warning_count} 个数据警告")
        
        # 数据量建议
        total_rows = sum(r.get('total_rows', 0) for r in self.check_results)
        if total_rows < 100:
            recommendations.append("📈 建议增加数据量，目标至少100条记录")
        elif total_rows < 500:
            recommendations.append("📈 建议继续扩充数据量，目标500+条记录")
        
        # 文件完整性建议
        missing_files = []
        for knowledge_type, filename in self.knowledge_files.items():
            file_path = self.knowledge_base_path / filename
            if not file_path.exists():
                missing_files.append(filename)
        
        if missing_files:
            recommendations.append(f"📁 创建缺失的文件: {', '.join(missing_files)}")
        
        return recommendations
    
    def save_report(self, report: Dict[str, Any], output_path: str = None):
        """保存质量报告"""
        if output_path is None:
            output_path = self.knowledge_base_path / f"quality_report_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
        
        with open(output_path, 'w', encoding='utf-8') as f:
            json.dump(report, f, ensure_ascii=False, indent=2)
        
        print(f"📄 质量报告已保存: {output_path}")
        return output_path
    
    def print_summary(self, report: Dict[str, Any]):
        """打印质量检查摘要"""
        summary = report['summary']
        
        print("\n" + "=" * 60)
        print("📊 数据质量检查摘要")
        print("=" * 60)
        print(f"📁 知识库路径: {report['knowledge_base_path']}")
        print(f"⏰ 检查时间: {report['timestamp']}")
        print(f"📄 检查文件: {summary['total_files']}个")
        print(f"✅ 通过验证: {summary['valid_files']}个")
        print(f"❌ 验证失败: {summary['invalid_files']}个")
        print(f"📊 质量评分: {summary['quality_score']}%")
        print(f"🚨 错误总数: {summary['total_errors']}个")
        print(f"⚠️  警告总数: {summary['total_warnings']}个")
        print(f"📝 数据总量: {summary['total_data_rows']}行")
        
        if report['recommendations']:
            print("\n💡 改进建议:")
            for i, rec in enumerate(report['recommendations'], 1):
                print(f"  {i}. {rec}")
        
        print("=" * 60)

def main():
    """主函数"""
    parser = argparse.ArgumentParser(description='数据质量检查工具')
    parser.add_argument(
        '--path', 
        default='/home/cunyu/intelligent-data-analysis/django_backend/data/knowledge_base',
        help='知识库路径'
    )
    parser.add_argument(
        '--output',
        help='报告输出路径'
    )
    parser.add_argument(
        '--quiet',
        action='store_true',
        help='静默模式，只输出摘要'
    )
    
    args = parser.parse_args()
    
    # 创建质量检查器
    checker = QualityChecker(args.path)
    
    # 运行质量检查
    report = checker.run_quality_check()
    
    # 保存报告
    if args.output:
        checker.save_report(report, args.output)
    else:
        checker.save_report(report)
    
    # 打印摘要
    if not args.quiet:
        checker.print_summary(report)
    else:
        summary = report['summary']
        print(f"质量评分: {summary['quality_score']}% ({summary['valid_files']}/{summary['total_files']} 文件通过)")

if __name__ == "__main__":
    main()
